package com.ybzl.mini.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ybzl.common.core.controller.IdGenerator;
import com.ybzl.common.core.domain.ResultVo;
import com.ybzl.common.core.domain.model.LoginUser;
import com.ybzl.common.utils.SecurityUtils;
import com.ybzl.mini.entity.*;
import com.ybzl.mini.entity.dto.DianZanDto;
import com.ybzl.mini.mapper.MiniBannersMapper;
import com.ybzl.mini.mapper.MiniFankuiMapper;
import com.ybzl.mini.mapper.MiniGoodPicsMapper;
import com.ybzl.mini.service.IMiniGoodService;
import com.ybzl.mini.service.IMiniPyqImgService;
import com.ybzl.mini.service.IMiniPyqService;
import com.ybzl.mini.service.IMiniWenzhangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * APP 客户端
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private IMiniWenzhangService iMiniWenzhangService;

    @Autowired
    private IMiniGoodService iMiniGoodService;

    @Autowired
    private MiniGoodPicsMapper miniGoodPicsMapper;

    @Autowired
    private MiniFankuiMapper miniFankuiMapper;

    @Autowired
    private MiniBannersMapper bannersMapper;


    @Autowired
    private IMiniPyqService iMiniPyqService;

    @Autowired
    private IMiniPyqImgService miniPyqImgService;


    /**
     * 新增反馈
     *
     * @return
     */
    @PostMapping("/v1/fankui/add")
    public ResultVo fankuiadd(@RequestBody MiniFankui fankui) {
        int a = 0;
        if (!ObjectUtils.isEmpty(fankui.getInfo())) {
            fankui.setId(IdGenerator.orderId(MiniFankui.class));
            fankui.setTime(new Date());
            LoginUser loginUser = SecurityUtils.getLoginUser();
            fankui.setUsername(loginUser.getUsername());
            fankui.setNickName(loginUser.getUser().getNickName());
            a = miniFankuiMapper.insert(fankui);
        }
        if (a > 0) {
            return ResultVo.success();
        } else {
            return ResultVo.error();
        }
    }

    /**
     * 所有文章
     *
     * @return
     */
    @PostMapping("/v1/wenzhang/list")
    public ResultVo wenzhangs(@RequestBody MiniWenzhang miniWenzhang) {
        LambdaQueryWrapper<MiniWenzhang> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MiniWenzhang::getSort);
        if (!ObjectUtils.isEmpty(miniWenzhang.getChannel())) {
            query.eq(MiniWenzhang::getChannel, miniWenzhang.getChannel());
        }
        if (!ObjectUtils.isEmpty(miniWenzhang.getTitle())) {
            query.like(MiniWenzhang::getTitle, miniWenzhang.getTitle());
        }
        query.eq(MiniWenzhang::getStatus, "0");
        return ResultVo.success(iMiniWenzhangService.list(query));
    }

    /**
     * 所有文章
     *
     * @return
     */
    @GetMapping("/v1/banner/list")
    public ResultVo banners() {
        LambdaQueryWrapper<MiniBanners> query = new LambdaQueryWrapper<MiniBanners>();
        query.eq(MiniBanners::getStatus,"0");
        query.orderByAsc(MiniBanners::getSort);
        return ResultVo.success(bannersMapper.selectList(query));
    }

    /**
     * 朋友圈列表
     *
     * @return
     */
    @GetMapping("/v1/pyq/list")
    public ResultVo pyq() {
        LambdaQueryWrapper<MiniPyq> query = new LambdaQueryWrapper<MiniPyq>();
        query.orderByDesc(MiniPyq::getTime);
        query.eq(MiniPyq::getStatus,"1");
        List<MiniPyq> pyqs = iMiniPyqService.list(query);

        pyqs.forEach(i -> {
            LambdaQueryWrapper<MiniPyqImg> miniPyqQueryWrapper = new LambdaQueryWrapper<MiniPyqImg>();
            miniPyqQueryWrapper.eq(MiniPyqImg::getFabuId, i.getId());
            i.setPics(miniPyqImgService.list(miniPyqQueryWrapper));

            Date d1 = i.getTime();
            Date d2 = DateUtil.date();

            Long oneDay = 86400L;
            Long twoDay = 2 * 86400L;
            Long sas = DateUtil.between(d1, d2, DateUnit.SECOND);
            if (sas > 0 && sas < oneDay) {
                // 刚刚
                i.setTimestr("刚刚");

            } else if (sas >= oneDay && sas < twoDay) {
                // 昨天
                i.setTimestr("昨天");
            } else {
                // 2天前
                i.setTimestr("2天前");
            }
        });
        return ResultVo.success(pyqs);
    }

    /**
     * 朋友圈点赞
     *
     * @return
     */
    @PostMapping("/v1/pyq/dianzan")
    public ResultVo dianzan(@RequestBody DianZanDto dianZanDto) {
            MiniPyq pyq = new MiniPyq();
            pyq.setZan(String.valueOf(Integer.valueOf(dianZanDto.getZan())+1));
            pyq.setId(dianZanDto.getId());
        return ResultVo.success(iMiniPyqService.updateById(pyq));
    }

    /**
     * 所有商品
     *
     * @return
     */
    @PostMapping("/v1/good/list")
    public ResultVo goods(@RequestBody MiniGood miniGood) {
        LambdaQueryWrapper<MiniGood> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MiniGood::getSort);
        if (!ObjectUtils.isEmpty(miniGood.getName())) {
            query.like(MiniGood::getName, miniGood.getName());
        }
        if (!ObjectUtils.isEmpty(miniGood.getType())) {
            query.like(MiniGood::getType, miniGood.getType());
        }
        if (!ObjectUtils.isEmpty(miniGood.getTypeTwo())) {
            query.like(MiniGood::getTypeTwo, miniGood.getTypeTwo());
        }
        query.eq(MiniGood::getStatus, "0");
        List<MiniGood> goods = iMiniGoodService.list(query);
        for (MiniGood tmp : goods) {
            List<MiniGoodPics> pics = miniGoodPicsMapper.selectList(new LambdaQueryWrapper<MiniGoodPics>().eq(MiniGoodPics::getGoodId, tmp.getId()));
            if (!ObjectUtils.isEmpty(pics)) {
                tmp.setUrl(pics.get(0).getUrl());
            }
        }

        return ResultVo.success(goods);
    }


    @GetMapping("/v1/wenzhang/detail/{keyId}")
    public ResultVo detail(@PathVariable("keyId") String keyId) {
        return ResultVo.success(iMiniWenzhangService.getById(keyId));
    }

    /**
     * @param type    一级分类
     * @param typeTwo 二级分类
     * @return
     */
    @GetMapping("/v1/good/detail")
    public ResultVo gdetail(@RequestParam("type") String type, @RequestParam("typeTwo") String typeTwo) {
        LambdaQueryWrapper query = new LambdaQueryWrapper<MiniGood>()
                .eq(MiniGood::getType, type)
                .eq(MiniGood::getTypeTwo, typeTwo);
        return ResultVo.success(iMiniGoodService.list(query));
    }

    /**
     * @param id 商品主键
     * @return
     */
    @GetMapping("/v1/good/info")
    public ResultVo goodDetail(@RequestParam("id") String id) {
        GoodsVo goodsVo = new GoodsVo();
        MiniGood good = iMiniGoodService.getById(id);
        goodsVo.setMiniGood(good);

        // 图片
        goodsVo.setUrls(miniGoodPicsMapper.selectList(new LambdaQueryWrapper<MiniGoodPics>().eq(MiniGoodPics::getGoodId, id)));
        return ResultVo.success(goodsVo);
    }
}
